import { API_LIST } from '../../../../Api/Api'
import { commonTableList, BuiltInData } from '../../Common/common'
import {
    AddDataButton,
    ModDataButton,
    DelDataButton,
    OpenTwoRouter,
    CommonButton,
    OpenExcelButton,
    AnnexButton,
    ExecuteButton,
} from '../../Common/options'
import dayjs from 'dayjs'

export const FlowForm = async ({ OptionsEvent, flow, selectType, actionRef }) => {
    const { uuid, isAmount } = flow

    const ClientData = JSON.parse(localStorage.getItem('ClientData'))
    const { userOrganizations } = JSON.parse(sessionStorage.getItem('UserInfo'))
    const [organizationUuid] = userOrganizations
    let data = {
        pageSize: 10,
        params: { pageIndex: 1, flowUuid: uuid, selectType },
        get: API_LIST()[`getFormGetFormByStatus`],
        added: `posForm`,
        modify: `putForm`,
        delete: `delForm`,
        initialValues: { approvalStatus: 0, flowUuid: uuid, organizationUuid, approvalCondition: 0 },
        options: [
            selectType === 'Draft' && (
                <AddDataButton OptionsEvent={OptionsEvent} type="primary" icon="add" title="新增" />
            ),
        ],
        columns: [
            commonTableList.uuid,
            { ...commonTableList.SearchSort, selectType: 'FlowForm' },
            {
                dataIndex: 'eventDate',
                title: '日期',
                valueType: 'Date',

                hideInSearch: true,
                rules: true,
                width: 200,
                render: (_, e) => <div>{dayjs(e.eventDate).format('YYYY-MM-DD')}</div>,
            },
            {
                ...commonTableList.organizationUuid,
                valueEnum: ClientData.organization,
                hideInAdded: true,
                hideInModify: true,
            },
            { ...commonTableList.tableName, title: '主题' },
            {
                dataIndex: 'approvalCondition',
                title: '金额',
                valueType: isAmount ? 'input' : 'hidden',
                pattern: '[0-9]*',
                tooltip: '如果审批类型不是是“按表单中金额审批”,则审批填0。由配置人员填写数据,都是小于等',

                rules: true,
                width: 200,
                hideInSearch: true,
                hideInTable: !isAmount,
            },
            {
                dataIndex: 'approvalStatus',
                title: '状态',
                valueType: 'select',
                valueEnum: BuiltInData.FormApprovalStatus,
                hideInAdded: true,
                hideInModify: true,

                rules: true,
                width: 200,
            },
            {
                dataIndex: 'infoContent',
                title: '内容',
                valueType: 'textarea',
                hideInSearch: true,

                width: 200,
            },
            {
                dataIndex: 'flowUuid',
                title: '流程Uuid',
                valueType: 'hidden',
                hideInSearch: true,
                hideInTable: true,
            },
            {
                dataIndex: 'organizationUuid',
                title: '组织机构',
                valueType: 'hidden',
                hideInSearch: true,
                hideInTable: true,
            },
            {
                dataIndex: 'approvalStatus',
                title: '状态',
                valueType: 'hidden',
                hideInSearch: true,
                hideInTable: true,
            },
            {
                title: '操作',
                valueType: 'systemOperation',
                fixed: 'right',
                width: 600,
                hideInSearch: true,
                hideInAdded: true,
                hideInModify: true,
                render: (_, record) => {
                    const { flowUuid, uuid, pdfUrl, isFormEdit, isFormExcel } = record
                    return [
                        selectType === 'Draft' && (
                            <ModDataButton OptionsEvent={OptionsEvent} key="modify" title="修改" data={record} />
                        ),
                        // 可编辑
                        ((selectType === 'Draft' && !!isFormExcel) || (selectType === 'Pending' && !!isFormEdit)) && (
                            <OpenExcelButton
                                key="open"
                                title="编辑表单"
                                OptionsEvent={OptionsEvent}
                                data={{
                                    submit: true,
                                    save: true,
                                    url: 'getFormGetApprovalFormEXCEL',
                                    pos: 'posFormUploadApprovalFormEXCEL',
                                    formUuid: uuid,
                                    flowUuid,
                                }}
                            />
                        ),
                        // 不可编辑
                        selectType === 'Pending' && !!isFormExcel && pdfUrl && (
                            <AnnexButton OptionsEvent={OptionsEvent} key="see1" url={pdfUrl} title="预览表单" />
                        ),
                        selectType !== 'Draft' && selectType !== 'Pending' && !!isFormExcel && pdfUrl && (
                            <AnnexButton OptionsEvent={OptionsEvent} key="see2" url={pdfUrl} title="预览表单" />
                        ),
                        <OpenTwoRouter
                            key="TwoOne"
                            title="表单附件"
                            twoTitle="表单附件"
                            modal="TableList"
                            OptionsEvent={OptionsEvent}
                            other={{
                                page: 'Form',
                                oneData: record,
                                header: data.columns,
                                ClientData,
                            }}
                        />,
                        <OpenTwoRouter
                            key="TwoTwo"
                            title="接收人员"
                            twoTitle="接收人员"
                            modal="TableList"
                            OptionsEvent={OptionsEvent}
                            other={{
                                page: 'FormConsigneeRecord',
                                oneData: record,
                                header: data.columns,
                                ClientData,
                            }}
                        />,
                        selectType === 'Draft' && (
                            <CommonButton
                                key="submit"
                                OptionsEvent={OptionsEvent}
                                title="提交表单"
                                data={{ type: 'FlowFormSubmitModal', record, width: 800, actionRef }}
                            />
                        ),
                        selectType === 'Draft' && (
                            <DelDataButton
                                OptionsEvent={OptionsEvent}
                                key="del"
                                title="删除"
                                url={'Form'}
                                uuid={record.uuid}
                            />
                        ),
                        <CommonButton
                            key="two"
                            OptionsEvent={OptionsEvent}
                            title="审批记录 "
                            data={{ type: 'FlowChartRecords', record, width: 1000, actionRef }}
                        />,
                        selectType === 'Pending' && (
                            <CommonButton
                                key="thr"
                                OptionsEvent={OptionsEvent}
                                title="审批"
                                smallTile="审批流程操作"
                                data={{ type: 'FlowChartApprovalStatus', record, width: 800, actionRef }}
                            />
                        ),
                        selectType === 'UnRead' && (
                            <ExecuteButton
                                actionRef={actionRef}
                                key="fou"
                                title="已读"
                                url="posFormConsigneeRecordSetFormConsigneeRecord"
                                path={[uuid]}
                                tips="已标记阅读!"
                            />
                        ),
                    ]
                },
            },
        ],
    }

    return data
}
